<?php

namespace App\DataTables;

use App\Models\Item;
use Form;
use Yajra\Datatables\Services\DataTable;

class ItemDataTable extends DataTable
{

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajax()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->addColumn('action', 'backend.items.datatables_actions')
            ->make(true);
    }

    /**
     * Get the query object to be processed by datatables.
     *
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
     */
    public function query()
    {
        $items = Item::query();

        return $this->applyScopes($items);
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->addAction(['width' => '150px', 'title' => trans('labels.general.actions')])
            ->ajax('')
            ->parameters([
                'dom' => 'Bfrtip',
                'scrollX' => false,
                'buttons' => [
                    'print',
                    'reset',
                    'reload',
                    //trans('labels.general.buttons.print'),
                    //trans('labels.general.buttons.reset'),
                    //trans('labels.general.buttons.reload'),
                    [
                         'extend'  => 'collection',
                         'text'    => '<i class="fa fa-download"></i> '.trans('labels.general.buttons.export'),
                         'buttons' => [
                             'csv',
                             'excel',
                             'pdf',
                         ],
                    ],
                    'colvis'
                ]
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    private function getColumns()
    {
        return [
            'name' => ['name' => 'name', 'data' => 'name'],
            'logo' => ['name' => 'logo', 'data' => 'logo'],
            'regtime' => ['name' => 'regtime', 'data' => 'regtime'],
            'country' => ['name' => 'country', 'data' => 'country'],
            'state_id' => ['name' => 'state_id', 'data' => 'state_id'],
            'state' => ['name' => 'state', 'data' => 'state'],
            'state_name' => ['name' => 'state_name', 'data' => 'state_name'],
            'county_id' => ['name' => 'county_id', 'data' => 'county_id'],
            'county' => ['name' => 'county', 'data' => 'county'],
            'county_name' => ['name' => 'county_name', 'data' => 'county_name'],
            'zip_id' => ['name' => 'zip_id', 'data' => 'zip_id'],
            'zip_code' => ['name' => 'zip_code', 'data' => 'zip_code'],
            'zip' => ['name' => 'zip', 'data' => 'zip'],
            'zip_name' => ['name' => 'zip_name', 'data' => 'zip_name'],
            'address' => ['name' => 'address', 'data' => 'address'],
            'introduction' => ['name' => 'introduction', 'data' => 'introduction'],
            'business' => ['name' => 'business', 'data' => 'business'],
            'brokerage' => ['name' => 'brokerage', 'data' => 'brokerage'],
            'web_url' => ['name' => 'web_url', 'data' => 'web_url'],
            'legal_person' => ['name' => 'legal_person', 'data' => 'legal_person'],
            'salesman_name' => ['name' => 'salesman_name', 'data' => 'salesman_name'],
            'salesman_phone' => ['name' => 'salesman_phone', 'data' => 'salesman_phone'],
            'salesman_wechat' => ['name' => 'salesman_wechat', 'data' => 'salesman_wechat'],
            'salesman_duty' => ['name' => 'salesman_duty', 'data' => 'salesman_duty']
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'items';
    }
}
